/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var latitude;
var longitude;

function mostrarMapaAd(mapButton)
{
    var data = $(mapButton).parent().siblings(".classLevelOneAdData").children();
    var nome = $(data[0]).html();
    var endereco = $(data[1]).html();
    var divMap = $(mapButton).parent().parent().next()[0];
    
    mostrarMapa(nome, endereco, divMap);
}


function mostrarMapaSimpleResult(mapButton)
{
    var data = $(mapButton).parent().siblings();
    var nome = $(data[0]).html();
    var endereco = $(data[1]).html();
    var divMap = $(mapButton).parent().parent().next()[0];
    
    mostrarMapa(nome, endereco, divMap);
}
function mostrarMapa(nome, endereco, divMap, mapButton)
{
    if($(divMap).is(":visible"))
    {
        $(mapButton).removeClass("classShowMapLinkActive");
        $(divMap).slideUp(200, function(){resizePageBackGround();});
    }
    else
    {
        $(mapButton).addClass("classShowMapLinkActive");
        $(divMap).hide();
        $(divMap).css("background-color", "litghgrey");
        $(divMap).css("width", "655px");
        $(divMap).css("height", "350px");
        $(divMap).slideDown(200, function(){
            getMapa(endereco, nome, divMap);
            resizePageBackGround();
        });
    }
    
}
function getMapa(endereco, nome, mapDiv) {
    
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': endereco + ', Brasil', 'region': 'BR'}, function(results, status) {

        if (status === google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                latitude = results[0].geometry.location.lat();
                longitude = results[0].geometry.location.lng();

                var myLatlng = new google.maps.LatLng(latitude, longitude);

                var marker = new google.maps.Marker({
                    position: myLatlng,
                    title: nome
                });

                var mapOptions = {
                    center: myLatlng,
                    zoom: 16,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                
                var map = new google.maps.Map(mapDiv,
                        mapOptions);
                            
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    title: nome
                });

                marker.setMap(map);
            }
        }
    });
}
